(function(a, ng) {
    'use strict';
    var app = a.adminApp;
    app.service('User', ['$http', '$q', function($http, $q) {
            var factory = {
                getUsers: function() {
                    return $http.get(URL_ADMIN + 'users');
                },
                getUser: function(id) {
                    var d = $q.defer();
                    $http({
                        url: URL_ADMIN + 'users',
                        'method': 'POST',
                        data: {
                            id: id
                        }
                    }).success(function(data, status, headers) {
                        d.resolve(data);
                    }).error(function(data, status, headers) {
                        d.reject(data);
                    });
                    return d.promise;
                }
            };

            return factory;
        }]);

    app.service('Comitentes', ['$http', '$q', function($http, $q) {
            var factory = {
                getAll: function() {
                    return $http.get(URL_ADMIN + 'comitentes');
                },
                get: function(id) {
                    var d = $q.defer();
                    $http({
                        url: URL_ADMIN + 'comitentes',
                        'method': 'POST',
                        data: {
                            id: id
                        }
                    }).success(function(data, status, headers) {
                        d.resolve(data);
                    }).error(function(data, status, headers) {
                        d.reject(data);
                    });
                    return d.promise;
                }
            };

            return factory;
        }]);

    app.service('ApiRest', ['$http', '$q', function($http, $q) {
            var factory = {
                getAllCondicoes: function() {
                    return $http.get('/api/condicoes');
                },
                get: function(id) {
                    var d = $q.defer();
                    $http({
                        url: URL_ADMIN + 'comitentes',
                        'method': 'POST',
                        data: {
                            id: id
                        }
                    }).success(function(data, status, headers) {
                        d.resolve(data);
                    }).error(function(data, status, headers) {
                        d.reject(data);
                    });
                    return d.promise;
                }
            };

            return factory;
        }]);

    app.service('Api', ['$http', '$q', function($http, $q) {
            var factory = {
                get: function(url, id) {
                    var d = $q.defer();
                    $http({
                        url: URL_ADMIN + 'comitentes',
                        'method': 'POST',
                        data: {
                            id: id
                        }
                    }).success(function(data, status, headers) {
                        d.resolve(data);
                    }).error(function(data, status, headers) {
                        d.reject(data);
                    });
                    return d.promise;
                }
            };

            return factory;
        }]);
    ng.module('service.breadcumbs', []);
    ng.module('service.breadcumbs').factory(
            'breadcrumbs',
            [
                '$rootScope',
                '$location',
                function($rootScope, $location) {

                    var breadcrumbs = [];
                    var breadcrumbsService = {};

                    // we want to update breadcrumbs only when a route is
                    // actually changed
                    // as $location.path() will get updated imediatelly
                    // (even if route change fails!)
                    $rootScope.$on('$routeChangeSuccess',
                            function(event, current) {

                                var pathElements = $location.path().split(
                                        '/'), result = [], i;
                                var breadcrumbPath = function(index) {
                                    return '/'
                                            + (pathElements.slice(0,
                                                    index + 1)).join('/');
                                };

                                pathElements.shift();
                                for (i = 0; i < pathElements.length; i++) {
                                    result.push({
                                        name: pathElements[i],
                                        path: breadcrumbPath(i)
                                    });
                                }

                                breadcrumbs = result;
                            });

                    breadcrumbsService.getAll = function() {
                        return breadcrumbs;
                    };

                    breadcrumbsService.getFirst = function() {
                        return breadcrumbs[0] || {};
                    };

                    return breadcrumbsService;
                }]);

    app.factory('cocGridDataFactory', ['$http', '$resource',
        function($http, $resource) {
            return function(type) {
                return $resource(base_url + 'api/' + type + '/:id', {
                    id: '@id'
                }, {
                    'update': {
                        method: 'PUT'
                    }
                }, {
                    'query': {
                        method: 'GET',
                        isArray: false
                    }
                });
            };
        }]);

    app
            .factory(
                    'notificationFactory',
                    function() {
                        return {
                            success: function(msg) {
                                var msg = msg
                                        || "Opera&ccedil;&atilde;o realizada com sucesso";
                                toastr.success(msg);
                            },
                            error: function(text) {
                                toastr
                                        .error(text,
                                                "A opera&ccedil;&atilde;o n&atilde;o foi concluida");
                            },
                            confirm: function(options) {

                                var o = {};

                                o.callbackConfirm = options.callbackConfirm
                                        || function() {
                                        };
                                o.callbackCancel = options.callbackCancel
                                        || function() {
                                        };
                                o.message = options.message || '';
                                o.afterShow = options.afterShow || function() {
                                }
                                o.loaded = options.Loeade || function(obj, e) {
                                }

                                var $form = '<div class="modal fade " id="compose-modal" tabindex="-1" role="dialog" aria-hidden="true">'
                                        + ' <div class="modal-dialog confirm">'
                                        + ' <div class="modal-content">'
                                        + '  <div class="modal-header">'
                                        + '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'
                                        + ' <h4 class="modal-title"><i class="fa fa-question-circle"></i> Sistema Pergunta</h4>'
                                        + ' </div>'
                                        + '<form action="#" method="post">'
                                        + ' <div class="modal-body">'
                                        + o.message
                                        + '</div>'
                                        + '<div class="modal-footer clearfix">'
                                        + ' <button type="button" class="btn icancel btn-danger" data-dismiss="modal"><i class="fa fa-times"></i> Cancelar</button>'
                                        + ' <button type="button" data-dismiss="modal" class="btn iconfirm btn-primary pull-left"><i class="fa fa-arrow-circle-right"></i> Confirmar</button>'
                                        + ' </div>'
                                        + ' </form>'
                                        + ' </div><!-- /.modal-content -->'
                                        + '</div><!-- /.modal-dialog -->'
                                        + '</div><!-- /.modal -->';
                                $('#compose-modal').remove();
                                $($form).appendTo('body');
                                $('#compose-modal').modal().on('shown.bs.modal', function(event) {
                                    o.afterShow(this, event);
                                }).on('loaded.bs.modal', function() {
                                    o.loaded(this, event);

                                });

                                $('#compose-modal').off('click.confirm').on(
                                        'click.confirm', 'button.iconfirm',
                                        o.callbackConfirm);
                                $('#compose-modal').off('click.cancel').on(
                                        'click.cancel', 'button.icancel',
                                        o.callbackCancel);

                            },
                            Options: function(options) {

                                var o = {};

                                o.callbackYes = options.callbackYes
                                        || function() {
                                        };
                                o.callbackNo = options.callbackNo
                                        || function() {
                                        };
                                o.callbackCancel = options.callbackCancel
                                        || function() {
                                        };
                                o.cancelLabel = options.cancelLabel || 'Cancelar';
                                o.message = options.message || '';
                                o.loaded = options.Loeaded || function(obj, e) {
                                }
                                 o.afterShow = options.afterShow || function() {
                                }

                                var $form = '<div class="modal fade " id="compose-modal" tabindex="-1" role="dialog" aria-hidden="true">'
                                        + ' <div class="modal-dialog confirm">'
                                        + ' <div class="modal-content">'
                                        + '  <div class="modal-header">'
                                        + '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'
                                        + ' <h4 class="modal-title"><i class="fa fa-question-circle"></i> Sistema Pergunta</h4>'
                                        + ' </div>'
                                        + '<form action="#" method="post">'
                                        + ' <div class="modal-body">'
                                        + o.message
                                        + '</div>'
                                        + '<div class="modal-footer clearfix">'
                                        + ' <button type="button" class="btn icancel btn-default" data-dismiss="modal"><i class="fa fa-times"></i> ' + o.cancelLabel + '</button>'
                                        + ' <button type="button" data-dismiss="modal" class="btn iyes btn-primary pull-left"><i class="fa fa-arrow-circle-right"></i> Sim</button>'
                                        + ' <button type="button" data-dismiss="modal" class="btn ino btn-danger pull-left"><i class="fa fa-arrow-circle-right"></i> Não </button>'
                                        + ' </div>'
                                        + ' </form>'
                                        + ' </div><!-- /.modal-content -->'
                                        + '</div><!-- /.modal-dialog -->'
                                        + '</div><!-- /.modal -->';
                                $('#compose-modal').remove();
                                $($form).appendTo('body');
                                $('#compose-modal').modal().on('shown.bs.modal', function(event) {
                                    o.afterShow(this, event);
                                }).on('loaded.bs.modal', function() {
                                    o.loaded(this, event);
                                });

                                $('#compose-modal').off('click.yes').on(
                                        'click.yes', 'button.iyes',
                                        o.callbackYes);
                                $('#compose-modal').off('click.no').on(
                                        'click.no', 'button.ino',
                                        o.callbackNo);
                                $('#compose-modal').off('click.cancel').on(
                                        'click.cancel', 'button.icancel',
                                        o.callbackCancel);

                            }
                            ,
                            modalBox: function(data) {
                                var buttons = data && data.buttons || {}, title = data
                                        && data.title || '', content = data
                                        && data.content || '', footer = data
                                        && data.footer || '',
                                        isBig = data && data.fullscreen != undefined ? 'modal-lg' : '';

                                var $form = '<div class="modal fade " id="compose-modal" tabindex="-1" role="dialog" aria-hidden="true">'
                                        + ' <div class="modal-dialog ' + isBig + '">'
                                        + ' <div class="modal-content">'
                                        + '  <div class="modal-header">'
                                        + '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'
                                        + ' <h4 class="modal-title">'
                                        + title
                                        + '</h4>'
                                        + ' </div>'
                                        + ' <div class="modal-body">'
                                        + content
                                        + ' </div>'
                                        + ' </div><!-- /.modal-content -->'
                                        + '</div><!-- /.modal-dialog -->'
                                        + '</div><!-- /.modal -->';
                                $('#compose-modal').remove();
                                $($form).appendTo('body');
                                $('#compose-modal').modal();
                            }
                        };
                    });

    /**
     * Brdate Filter
     * 
     * @Param str
     * @return string
     */
    app
            .filter(
                    'brdate',
                    function() {

                        return (function(str) {
                            var pdate = /^[0-9]{2}\-[0-9]{2}\-[0-9]{4}$/gi, pdatetime = /^[0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/gi, $date, $arrayDate;
                            if (pdatetime.test(str)) {
                                $arrayDate = str.split(' ');
                                $date = $arrayDate['0'].split('-');
                                $date = $date.reverse();
                                $date = $date.join('/') + ' ' + $arrayDate['1'];
                                return $date;
                            }

                        });

                    });
    app
            .service(
                    'browser',
                    [
                        '$window',
                        function($window) {

                            var userAgent = $window.navigator.userAgent, identify_user_agent = null;
                            var browsers = {
                                chrome: /chrome/i,
                                safari: /safari/i,
                                firefox: /firefox/i,
                                ie: /internet explorer/i
                            };
                            ng.forEach(browsers, function(key, value) {
                                if (key.test(userAgent)
                                        && identify_user_agent === null) {
                                    identify_user_agent = value;
                                }
                                ;
                            });
                            return {
                                agent: function() {
                                    return identify_user_agent;
                                }
                            };
                        }]);




   app.service('uploadService',['$http',function($http){
           this.run = function(file,url,name){
               var fd = new FormData(),
                   inFile = name || 'file';
               fd.append(inFile,file);
               $http.post(url,fd,{
                   transformRequest:angular.identify,
                   headers:{'Content-type':undefined}
               });
            };
   }]);
})(cocApp, angular);
